package com.digital.herbarium.Util;

import com.digital.herbarium.Entity.Menu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeUtil {


    // 递归函数，构建子节点
    public static   List<Map<String, Object>> buildChildMenuTree(int parentId, List<Menu> menuList) {
        List<Map<String, Object>> childList = new ArrayList<>();

        for (Menu m : menuList) {
            if (m.getParentId() == parentId) {
                Map<String, Object>  node = createNode(m, menuList);
                childList.add(node);
            }
        }

        return childList;
    }



    public static  Map<String, Object> createNode(Menu m, List<Menu> menuList){
        Map<String, Object> node = new HashMap<>();
        node.put("value", m.getMenuKey());
        node.put("key", m.getMenuKey());
        node.put("title", m.getMenuName());
        node.put("children", buildChildMenuTree(m.getId(), menuList));
        return node;
    }
}
